using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace InvoiceProject.Models
{ 
    public class InvoiceRepository : IInvoiceRepository
    {
        InvoiceProjectContext context = new InvoiceProjectContext();

        public IQueryable<Invoice> All
        {
            get { return context.Invoices; }
        }

        public IQueryable<Invoice> AllIncluding(params Expression<Func<Invoice, object>>[] includeProperties)
        {
            IQueryable<Invoice> query = context.Invoices;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Invoice Find(long id)
        {
            return context.Invoices.Include("Customer").Where(invoice => invoice.Id == id).FirstOrDefault();
        }

        public void InsertOrUpdate(Invoice invoice)
        {
            

            if (invoice.Id == default(long)) {
                // New entity
                context.Invoices.Add(invoice);
            } else {
                // Existing entity
                context.Entry(invoice).State = EntityState.Modified;
            }
        }

        public void Delete(long id)
        {
            var invoice = context.Invoices.Find(id);
            context.Invoices.Remove(invoice);
        }
        public Business CurrentBusinessForUser(string userName)
        {
            return context.Businesses.Include("Address").Where(business => business.UserName == userName).FirstOrDefault();
        }

        public void ChartData(string userName)
        {
          //  var invoiceList = context.Invoices.Where(p => p.Business.UserName == userName).ToList();
         //   invoiceList.


        }

        public void Save()
        {

    

            context.SaveChanges();
        }
    }

    public interface IInvoiceRepository
    {
        IQueryable<Invoice> All { get; }
        IQueryable<Invoice> AllIncluding(params Expression<Func<Invoice, object>>[] includeProperties);
        Invoice Find(long id);
        void InsertOrUpdate(Invoice invoice);
        void Delete(long id);
        void Save();
        Business CurrentBusinessForUser(string userName);
    }
}